//
//  GAKKIBaseTableViewHeaderFooterView.m
//  GANGAIKit
//
//  Created by fh on 2019/11/6.
//  Copyright © 2019 cashchina. All rights reserved.
//

#import "GAKKIBaseTableViewHeaderFooterView.h"

@implementation GAKKIBaseTableViewHeaderFooterView

#pragma mark - Public

/// 快速创建可重用的头尾工厂类方法:子类调用,使用类名作为标识符
/// @param tableView tableView
+ (instancetype)dequeueReusableHeaderFooterViewWithTable:(UITableView *)tableView {
    return [self dequeueReusableHeaderFooterViewWithTable:tableView reusableIdentifier:nil shouldAutoSetupSubViews:YES];
}

/// 快速创建可重用的头尾工厂类方法:子类调用
/// @param tableView tableView
/// @param identifier 重用标识符
/// @param shouldAutoSetupSubViews 是否自动调用setupView进行布局,为YES子类只需要实现setupView方法即可
+ (instancetype)dequeueReusableHeaderFooterViewWithTable:(UITableView *)tableView reusableIdentifier:(NSString *)identifier shouldAutoSetupSubViews:(BOOL)shouldAutoSetupSubViews {
    NSString *headerFooterViewName = [NSString stringWithUTF8String:object_getClassName(self)];
    Class newclass = NSClassFromString(headerFooterViewName);
    if ([newclass isSubclassOfClass:[GAKKIBaseTableViewHeaderFooterView class]]) {
        newclass = NSClassFromString(headerFooterViewName);
    }else {
        newclass = [self class];
    }
    NSString *reusableIdentifier = nil;
    
    BOOL ifNullResult = (identifier == nil || [identifier isKindOfClass:[NSNull class]] || [identifier length] < 1 || [identifier isEqualToString:@"(null)"] || [identifier isEqualToString:@"<null>"] || [identifier isEqualToString:@"<Null>"] ? YES : NO );
    
    if (!ifNullResult) {
        reusableIdentifier = identifier;
    }else {
        reusableIdentifier = headerFooterViewName;
    }
    GAKKIBaseTableViewHeaderFooterView *headerFooterView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:reusableIdentifier];
    if (headerFooterView == nil) {
        headerFooterView = [[newclass alloc] initWithReuseIdentifier:reusableIdentifier];
    }
    if (shouldAutoSetupSubViews) {
        [headerFooterView setupView];
    }
    return headerFooterView;
}

/// 配置子视图
- (void)setupView {
    // 子类实现
}

/// 清除所有子视图
- (void)cleanAllSubviews {
    for (UIView *subview in self.contentView.subviews) {
        [subview removeFromSuperview];
    }
}

#pragma mark - Setter and getter
- (void)setCustomBackgroundColor:(UIColor *)customBackgroundColor {
    _customBackgroundColor = customBackgroundColor;
    UIView *view = [UIView new];
    view.backgroundColor = customBackgroundColor;
    self.backgroundView = view;
}

- (void)dealloc {
    if (kDebugReusableViewRelease) {
#ifdef DEBUG
        DBLog(@"\n ----------- 可重用view %@ 销毁了 ----------- \n",[self class]);
#endif
    }
}
@end
